﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WebMainSite
{
    public partial class MainPage :BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //隐藏用来修改字段的div
                EditDiv.Visible = false;
                InitDataGridView();
            }
        }
        /// <summary>
        /// 初始化GridView的数据
        /// </summary>
        private void InitDataGridView()
        {
            MySite.BLL.SecretKey_ID secretKey_IDBll = new MySite.BLL.SecretKey_ID();
            DataSet ds = secretKey_IDBll.GetListUnion();
            GridView1.DataSource = ds;
            GridView1.DataKeyNames = new string[] { "SecretKey" };
            GridView1.DataBind();
        }
        /// <summary>
        /// 要求分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            InitDataGridView();
        }
        /// <summary>
        /// 要求排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        //{
        //}
        /// <summary>
        /// 判断这个密钥是否被真正使用（如果在Base_Dept表里面有这个密钥对应的企业代号，那么就表示已经真正被使用，
        /// 如果这个密钥给了企业，而企业还没使用的话，那么在Base_Dept表里面没有改企业号，就表示还没真正被使用）
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        //public string BeUsed(string name)
        //{
        //    if (!string.IsNullOrEmpty(name))
        //        return "是";
        //    else
        //        return "否";
        //}

        /// <summary>
        /// 生产密钥-企业ID
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtCreateKey_Click(object sender, EventArgs e)
        {
            LbMessage.Text = "";
            if (string.IsNullOrEmpty(num.Value))
                return;
            MySite.BLL.SecretKey_ID secretKey_IDBll = new MySite.BLL.SecretKey_ID();
            MySite.Model.SecretKey_ID secretKey_ID = new MySite.Model.SecretKey_ID();
            /*获取最大的企业号，例如：如果是0001-0010中间的005,006被删掉了，那么再分配的时候还是从0011开始
             * 被删掉的就不再计算了。ps:这个不是最好的方法，暂时这样处理先
            */
            int maxNum = secretKey_IDBll.GetMaxEnterpriserID();//secretKey_IDBll.GetRecordCount("");
            int tempNum = Convert.ToInt32(num.Value);
            if (maxNum + tempNum > 9999)
            {
                LbMessage.Text = "<div class='box box-error'>添加不成功，原因：若这样做，已经超出了数据库的最大记录9999，现在数据库已有" + maxNum + "条记录</div>";
                return;
            }
            for (int a = 1; a <= tempNum; a++)
            {
                //按规则生成 企业ID号
                secretKey_ID.EnterpriserID = (maxNum + a).ToString().PadLeft(4, '0');//在前面补0
                secretKey_IDBll.Add(secretKey_ID);
            }
            LbMessage.Text = "<div class='box box-success'>成功生成：密钥-企业ID</div>";
            num.Value = "";
            InitDataGridView();
        }

        protected void BtChange_Click(object sender, EventArgs e)
        {
            try
            {
                MySite.BLL.SecretKey_ID secretKey_IDBll = new MySite.BLL.SecretKey_ID();
                MySite.Model.SecretKey_ID secretKey_ID = new MySite.Model.SecretKey_ID();
                secretKey_ID.SecretKey = new Guid(miyue.Value);
                secretKey_ID.EnterpriserID = qiyeid.Value;
                secretKey_ID.AssignTo = yufenpei.Value;
                secretKey_IDBll.Update(secretKey_ID);
                LbChangeMessage.Text = "<div class='box box-success'>修改成功！</div>";

                InitDataGridView();
            }
            catch (Exception ex)
            {
                LbChangeMessage.Text = "<div class='box box-error'>修改失败！原因如下：" + ex.Message+"</div>";
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //如果是绑定数据行 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('删除操作不可逆，你确认要删除密钥：\"" + e.Row.Cells[0].Text.ToUpper() + "\"吗?')");
                }
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string secretKey = GridView1.DataKeys[e.RowIndex].Value.ToString();
            MySite.BLL.SecretKey_ID secretKey_IDBll = new MySite.BLL.SecretKey_ID();
            secretKey_IDBll.Delete(new Guid(secretKey));
            InitDataGridView();
            LbMessage.Text = "";
            EditDiv.Visible = false;
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            EditDiv.Visible = true;
            //密钥
            miyue.Value = GridView1.Rows[e.NewSelectedIndex].Cells[0].Text;
            //企业ID
            qiyeid.Value = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
            //预分配给
            yufenpei.Value = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text == "&nbsp;" ? "" : GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
            //实际使用该密钥的企业
            shijishiyong.Value = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text == "&nbsp;" ? "" : GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
        }
    }
}